#ifndef _PACKAGECATCHER_H_
#define _PACKAGECATCHER_H_

#include <pcap.h>
#include <string>
#include <vector>
#include <DeviceInfo.h>
#include <NetPackage.h>
#include <functional>
#include <mutex>
#include <condition_variable>

namespace wzp {

	using namespace std::placeholders;
	typedef std::function<void(unsigned char*, const struct pcap_pkthdr*, const unsigned char*)> packetCallBack;

	enum class CatcherState
	{
		unknow = 0,
		init,
		initDev,
		loop,
		end
	};

	class PackageCatcher {
	public:
		void initNpcap();
		void initNpcapDevice(int devIndex, const std::string& filter = "");
		void endNpcapLoop();
		void startNpcapLoop();
		int startCapture(int devIndex);
		//void packetHandler(unsigned char* param, const struct pcap_pkthdr* header, const unsigned char* pkt_data);

		std::vector<DeviceInfo> getDeviceInfos() const {
			return devList_;
		}
		CatcherState getState() const {
			return state_;
		}

		~PackageCatcher();
	private:
		pcap_if_t* ifaces_ = NULL;
		pcap_t* adhandle = NULL;
		pcap_dumper_t* dumpfile = NULL;
		char errbuf_[64] = { 0 };
		CatcherState state_ = wzp::CatcherState::unknow;

		std::vector<DeviceInfo> devList_;
		std::mutex lock_;
		std::condition_variable cv_;
	};
}

#endif // !_PACKAGECATCHER_H_
